package cn.phub.system.service.impl;

import cn.phub.basic.service.impl.BaseServiceImpl;
import cn.phub.system.domain.Role;
import cn.phub.system.mapper.RoleMapper;
import cn.phub.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jijie
 * @since 2022-04-06
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void add(Role role) {
        roleMapper.save(role);//返回自增Id
        //保存关联表的信息
        initRelationTable(role);
    }

    @Override
    public void update(Role role) {
        super.update(role);
    }

    private void initRelationTable(Role role) {
        //对于中间表的数据操作，建议先删除所有，再录入新的所有
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.removeRolePermisison(role.getId());

        //保存role_menu
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        //role_permisison
        roleMapper.saveRolePermission(role.getId(),role.getPermissions());
    }


}
